package com.zc.app.sys.service;

import com.github.pagehelper.PageInfo;
import com.zc.app.sys.dao.mapper.UserMapper;
import com.zc.app.sys.dao.peer.Menu;
import com.zc.app.sys.dao.peer.User;
import com.zc.framework.cache.CacheManager;
import com.zc.framework.shiro.RealmEventManager;
import com.zc.framework.shiro.bean.Principal;
import com.zc.framework.shiro.cas.CasMatcher;
import com.zc.framework.shiro.cas.Digests;
import com.zc.framework.shiro.cas.Encodes;
import com.zc.framework.spring.SpringContextHolder;
import com.zc.framework.utils.MD5Utils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zc on 2016/8/31.
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public int createUser(User user) {
        user.setLoginPassword(MD5Utils.getMD5(user.getLoginPassword()));
        int userId = userMapper.createUser(user);
//        RealmEventManager.getInstance().clearAllCachedAuthorizationInfo();
        return userId;
    }

    public User queryUserById(Integer accountId) {
        return userMapper.queryUserById(accountId);
    }

    public User findByUsername(String loginName){
        return userMapper.findByUsername(loginName);
    }


    public PageInfo<User> queryUser(User user){
        RowBounds rowBounds = new RowBounds(1, 10);
        return new PageInfo<User>(userMapper.queryUser(user,rowBounds));
    }
//
//    public User findByUsername(String loginName) {
//        return userMapper.findByUsername(loginName);
//    }
//
//    public List<Menu> getAuthorizedMenusByUserId(String userId){
//        return userMapper.getAuthorizedMenusByUserId(userId);
//    }

    public int updateUserLoginInfo(String loginName){
        return 0;
    }

    public User getCurrentUser(){
        User user = null;

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal)subject.getPrincipal();
        if(principal != null) {
//            user = getUser(principal.getId());
//            CacheManager.putCache("session_usr", user);
        }

        if(user == null) {
            user = new User();
            try {
                SecurityUtils.getSubject().logout();
            } catch (UnavailableSecurityManagerException var4) {
            } catch (InvalidSessionException var5) {
            }
        }

        return user;
    }

//    public User getUser(String userId) {
//        return userMapper.getUserById(userId);
//    }

    public boolean validatePassword(String plainPassword, String password) {
//        CasMatcher casMatcher = SpringContextHolder.getBean(CasMatcher.class);
//        byte[] salt = Encodes.decodeHex(password.substring(0, casMatcher.getSaltSize() * 2));
//        byte[] hashPassword = Digests.digest(plainPassword.getBytes(), casMatcher.getAlgorithm(), salt, casMatcher.getHashIterations());
//        return password.equals(Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword));
        return true;
    }

    public String entryptPassword(String plainPassword) {
//        CasMatcher casMatcher = SpringContextHolder.getBean(CasMatcher.class);
//        byte[] salt = Digests.generateSalt(casMatcher.getSaltSize());
//        byte[] hashPassword = Digests.digest(plainPassword.getBytes(), casMatcher.getAlgorithm(), salt, casMatcher.getHashIterations());
//        return Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword);
        return null;
    }


}
